package com.milkshopbackend.milkshop.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.milkshopbackend.milkshop.Service.OrderService;
import com.milkshopbackend.milkshop.common.Result;
import com.milkshopbackend.milkshop.entity.Order;
import com.milkshopbackend.milkshop.entity.OrderCreateDTO;
import com.milkshopbackend.milkshop.entity.OrderQueryDTO;
import com.milkshopbackend.milkshop.entity.OrderReviewDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/orders")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @PostMapping
    public Result<Order> createOrder(@RequestBody OrderCreateDTO orderDTO) {
        Order order = orderService.createOrder(orderDTO);
        return Result.success(order);
    }

    @GetMapping("/{orderId}")
    public Result<Order> getOrderDetail(@PathVariable Long orderId) {
        Order order = orderService.getOrderById(orderId);
        return Result.success(order);
    }

    @DeleteMapping("/{orderId}")
    public Result<Void> cancelOrder(@PathVariable Long orderId,@RequestBody String reason) {
        orderService.cancelOrder(orderId,reason);
        return Result.success();
    }


    @GetMapping
    public Result<Page<Order>> listOrders(
            @RequestParam(required = false) Integer page,
            @RequestParam(required = false) Integer size,
            @RequestParam(required = false) String status
    ) {
        OrderQueryDTO queryDTO = new OrderQueryDTO();
        queryDTO.setPage(page != null ? page : 1);
        queryDTO.setSize(size != null ? size : 10);

        // 字符串转状态码
        if (status != null) {
            switch (status) {
                case "undelivered": // 待发货
                    queryDTO.setStatus(com.milkshopbackend.milkshop.enums.OrderStatus.PAID.getCode());
                    break;
                case "unpaid": // 待支付
                    queryDTO.setStatus(com.milkshopbackend.milkshop.enums.OrderStatus.PENDING_PAYMENT.getCode());
                    break;
                case "shipped": // 已发货
                    queryDTO.setStatus(com.milkshopbackend.milkshop.enums.OrderStatus.SHIPPED.getCode());
                    break;
                case "completed": // 已完成
                    queryDTO.setStatus(com.milkshopbackend.milkshop.enums.OrderStatus.COMPLETED.getCode());
                    break;
                case "cancelled": // 已取消
                    queryDTO.setStatus(com.milkshopbackend.milkshop.enums.OrderStatus.CANCELLED.getCode());
                    break;
                default:
                    queryDTO.setStatus(null);
            }
        }

        Page<Order> pageResult = orderService.listOrders(queryDTO);
        return Result.success(pageResult);
    }

    @PostMapping("/{orderId}/pay")
    public Result<Void> payOrder(@PathVariable Long orderId, @RequestParam Integer paymentMethod) {
        orderService.payOrder(orderId, paymentMethod);
        return Result.success();
    }

    @PostMapping("/{orderId}/ship")
    public Result<Void> shipOrder(@PathVariable Long orderId,
                                  @RequestParam String trackingNo,
                                  @RequestParam String shippingCompany) {
        orderService.shipOrder(orderId, trackingNo, shippingCompany);
        return Result.success();
    }

    @PostMapping("/{orderId}/confirm")
    public Result<Void> confirmOrder(@PathVariable Long orderId) {
        orderService.confirmOrder(orderId);
        return Result.success();
    }

    @PostMapping("/{orderId}/review")
    public Result<Void> reviewOrder(@PathVariable Long orderId, @RequestBody OrderReviewDTO reviewDTO) {
        orderService.reviewOrder(orderId, reviewDTO);
        return Result.success();
    }
}